﻿using Engine;
using Game;

namespace CreatorWandModAPI
{
    public class NewReplace
    {
        public static int Replace(int Src_Param, int Des_Param, Point3 Start, Point3 End)
        {
            int num = 0;
            CreatorMain.Math.StarttoEnd(ref Start, ref End);
            //bool flag = false;
            bool Outflagcox = Des_Param >= 1024 || Src_Param < 0;

            for (int i = Start.Y; i <= End.Y; i++)
            {
                for (int j = Start.Z; j <= End.Z; j++)
                {
                    for (int k = Start.X; k <= End.X; k++)
                    {
                        if (Src_Param != GameManager.Project.FindSubsystem<SubsystemTerrain>(throwOnError: true).Terrain.GetCellContentsFast(k, i, j)) continue;
                        GameManager.Project.FindSubsystem<SubsystemTerrain>(throwOnError: true).ChangeCell(k, i, j,
                            Outflagcox
                                ? Terrain.MakeBlockValue(Terrain.ExtractContents(Des_Param), Terrain.ExtractLight(Des_Param), Terrain.ExtractData(Des_Param))
                                : Terrain.MakeBlockValue(Des_Param, GameManager.Project.FindSubsystem<SubsystemTerrain>(throwOnError: true).Terrain.GetCellLight(k, i, j), 0));

                        num++;
                    }
                }
            }

            return num;
        }
    }
}